/*
 * Date         Author      Note
 * 2014.12.01   舞者无罪            摇床参数界面
 * 2015.06.24   wzwz        增加返回按钮
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "lang.h"
#include "resource.h"
#include "func.h"
#include "mhbtn.h"
#include "mhlabel.h"
#include "rock.h"
#include "rock_pos.h"
#include "rock_tran.h"
#include "com.h"

/*
 *
 */

#define ROCK_CAPTION_LEFT    (30)
#define ROCK_CAPTION_TOP     (60)
#define ROCK_CAPTION_WIDTH   (200)
#define ROCK_CAPTION_HEIGHT  (30)



/*
 *
 */
static void CreateParamInterface(HWND hWnd)
{
    CreateWindow (MH_BTN_NAME,
        MW_ROCK1_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_ROCK_POS,
        ROCK_CAPTION_LEFT, ROCK_CAPTION_TOP, ROCK_CAPTION_WIDTH, ROCK_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_ROCK2_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_TRAN_ROCK,
        ROCK_CAPTION_LEFT, ROCK_CAPTION_TOP + ROCK_CAPTION_HEIGHT,
        ROCK_CAPTION_WIDTH, ROCK_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_ROCK3_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_PLUS_TRAN_ROCK,
        ROCK_CAPTION_LEFT, ROCK_CAPTION_TOP + ROCK_CAPTION_HEIGHT * 2,
        ROCK_CAPTION_WIDTH, ROCK_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_ROCK4_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_DEC_TRAN_ROCK,
        ROCK_CAPTION_LEFT, ROCK_CAPTION_TOP + ROCK_CAPTION_HEIGHT * 3,
        ROCK_CAPTION_WIDTH, ROCK_CAPTION_HEIGHT,
        hWnd, 0);
    char esc[64] = {0};
    sprintf(esc, GetString(802), 5);
    CreateWindow(MH_BTN_NAME,
        esc,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_ESC,
        30,
        60 + 30 * 4,
        200,
        30,
        hWnd, 0);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    switch(nID)
    {
    case ID_TRAN_ROCK:
        if(STATE_STOP == GetRunState(g_knit_data))
        {
            CreateRockTranWindow(hWnd);
        }
        break;
    case ID_PLUS_TRAN_ROCK:
        {
        }
        break;
    case ID_DEC_TRAN_ROCK:
        {
        }
        break;
    case ID_ROCK_POS:
        if(STATE_STOP == GetRunState(g_knit_data))
        {
            CreateRockPosWindow(hWnd);
        }
        break;
    case ID_ESC:
        {
            PostMessage (hWnd, MSG_CLOSE, 0, 0);
        }
        break;
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE || key == SCANCODE_5)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_1)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_ROCK_POS));
        OnBtnCmd(hWnd, ID_ROCK_POS);
    }
    else if(key == SCANCODE_2)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_TRAN_ROCK));
        OnBtnCmd(hWnd, ID_TRAN_ROCK);
    }
    else if(key == SCANCODE_3)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_PLUS_TRAN_ROCK));
    }
    else if(key == SCANCODE_4)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_DEC_TRAN_ROCK));
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        uint nID = 0;
        switch(GetDlgCtrlID(GetFocusChild(hWnd)))
        {
        case ID_TRAN_ROCK:
            nID = ID_ROCK_POS;
            break;
        case ID_PLUS_TRAN_ROCK:
            nID = ID_TRAN_ROCK;
            break;
        case ID_DEC_TRAN_ROCK:
            nID = ID_PLUS_TRAN_ROCK;
            break;
        case ID_ESC:
            nID = ID_DEC_TRAN_ROCK;
            break;
        default:
            break;
        }
        if(nID)
        {
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        uint nID = 0;
        switch(GetDlgCtrlID(GetFocusChild(hWnd)))
        {
        case ID_ROCK_POS:
            nID = ID_TRAN_ROCK;
            break;
        case ID_TRAN_ROCK:
            nID = ID_PLUS_TRAN_ROCK;
            break;
        case ID_PLUS_TRAN_ROCK:
            nID = ID_DEC_TRAN_ROCK;
            break;
        case ID_DEC_TRAN_ROCK:
            nID = ID_ESC;
            break;
        default:
            break;
        }
        if(nID)
        {
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_ENTER)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        switch(nID)
        {
        case ID_ROCK_POS:
            OnBtnCmd(hWnd, nID);
            break;
        case ID_TRAN_ROCK:
            OnBtnCmd(hWnd, nID);
            break;
        case ID_ESC:
            OnBtnCmd(hWnd, nID);
            break;
        default:
            break;
        }
    }
}
/*
 *
 */
static int RockWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateParamInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += MASTER_TITLE_LEFT;
            rect.top += MASTER_TITLE_TOP;
            rect.right = rect.left + MASTER_TITLE_WIDTH - 1;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        DestroyMainWindow(hWnd);
        MainWindowCleanup (hWnd);
        return 0;
    case MSG_KEYDOWN:
        OnKeyDown(hWnd, wParam);
        return 0;
    case MSG_COMMAND:
        OnBtnCmd(hWnd, LOWORD(wParam));
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls (hWnd);
        }
        break;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_MAST]);
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateRockWindow(HWND hHosting)
{
    HWND hWnd;
    MAINWINCREATE CreateInfo;

    int x = (CX_SCREEN - MASTER_WND_CX) / 2;
    int y = (CY_SCREEN - MASTER_WND_CY) / 2;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_PARAM_ROCK_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = RockWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + MASTER_WND_CX;
    CreateInfo.by = y + MASTER_WND_CY;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_ROCK_POS));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    else
    {
    }
    return hWnd;
}
